<template>
	<view class="normal-login-container">
		<view class="top_img">
			<view class="">
				您好，
			</view>
			<view class="">
				欢迎使用智慧订餐
			</view>
		</view>

		<view class="login-form-content">
			<view class="input-item flex align-center">
				<!-- <view class="iconfont icon-user icon"></view> -->
				<!-- <view class="label">+86</view> -->
				<input v-model="loginForm.mobile " class="input" type="text" placeholder="请输入手机号" maxlength="30" />
				<view class="u-demo-block__content">
					<!-- <u-code ref="uCode" @change="codeChange" keep-running start-text="获取验证码"></u-code> -->
					<text @tap="getCodeNum" :text="tips" class="u-page__code-text">{{tips}}</text>
				</view>
			</view>
			<view class="input-item flex align-center">
				<input v-model="loginForm.verificationCode" class="input" type="text" placeholder="请输入验证码"
					maxlength="30" />
			</view>
			<view class="">
				<u-radio-group v-model="radiovalue" placement="row" @change="gsroupChange">
					<u-radio activeColor="#0EB275" :customStyle="{marginBottom: '8px',marginRight:'50rpx'}"
						v-for="(item, index) in radiolist" :key="index" :label="item.name" :name="item.name">
					</u-radio>
				</u-radio-group>
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-green lg round ">登录</button>
				<!-- <button @click="quickLogin" class="login-btn cu-btn block bg-blue lg round">手机号快捷登录</button> -->
				<!-- <button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"
					class="login-btn cu-btn block bg-blue lg round">手机号快捷登录</button>
				<button open-type="getUserInfo" @click="getUserProfile"
					class="login-btn cu-btn block bg-blue lg round">登录</button> -->
			</view>
			<!-- 			<view class="reg text-center" v-if="register">
				<text class="text-grey1">没有账号？</text>
				<text @click="handleUserRegister" class="text-blue">立即注册</text>
			</view>
			<view class="xieyi text-center">
				<text class="text-grey1">登录即代表同意</text>
				<text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
				<text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
			</view> -->
		</view>

	</view>
</template>

<script>
	import {
		getCodeImg
	} from '@/api/login'

	export default {
		data() {
			return {

				tips: '',
				// 基本案列数据
				radiolist: [{
						name: '员工',
						disabled: false
					},
					{
						name: '管理员',
						disabled: false
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				radiovalue: '员工',
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: false,
				globalConfig: getApp().globalData.config,
				loginForm: {
					// nickname: "",
					// username: "",
					// password: "",
					// code: "",
					// uuid: "",
					mobile: '',
					verificationCode: ''

				},
				loginCode: ''
			}
		},
		created() {
			// console.log('vue');
			// console.log(uni.$u.config.v);
			// this.getCode()
			this.getAppCode()

		},
		methods: {
			gsroupChange(e) {
				console.log(e)
			},
			// radioChange(e) {
			// 	console.log(e)
			// },
			async getUserProfile() {
				const ress = await uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						console.log(res)
					},
					fail: (res) => {
						console.log(res)
					}

				})
				console.log(ress);
			},
			async onGetPhoneNumber(e) {

				const {
					encryptedData,
					iv,
					errMsg,
					code
				} = e.detail;
				if (errMsg !== 'getPhoneNumber:ok') {
					uni.showToast({
						title: '授权失败',
						icon: 'none'
					});
					return;
				}
				// 拿到加密数据和iv
				// 发送到后端解密并登录
				const res = await uni.request({
					url: 'https://你的后端域名/api/wx-login',
					method: 'POST',
					data: {
						code: this.loginCode, //openid和手机号绑定到一块
						// code,
						encryptedData,
						iv,
					},
				});

				if (res.data.success) {
					uni.setStorageSync('token', res.data.token); // 保存登录态
					uni.reLaunch({
						url: '/pages/index/index'
					}); // 跳转首页
				} else {
					uni.showToast({
						title: '登录失败',
						icon: 'none'
					});
				}
			},

			async getAppCode() {
				// const loginRes = await uni.login(
				// 	// {
				// 	// 	provider: 'weixin'
				// 	// }
				// )
				const res = await uni.login()
				// console.log('res');
				// console.log(res[1].code);
				if (res && res.length > 0) {
					this.loginCode = res[1].code;
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			getCodeNum() {
				console.log(this.$refs.uCode.canGetCode);
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						// uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				uni.$u.toast('倒计时开始');
			},
			// 用户注册
			handleUserRegister() {
				this.$tab.redirectTo(`/pages/register`)
			},
			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.img
						this.loginForm.uuid = res.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入密码")
				} else if (this.loginForm.code === "" && this.captchaEnabled) {
					this.$modal.msgError("请输入验证码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch(() => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					// this.$tab.reLaunch('/pages/index')
					const role = 'admin'
					if (role === 'admin') {
						uni.redirectTo({
							url: '/subpackages/admin/pages/index/index'
						})
					} else {
						uni.redirectTo({
							url: '/pages/index'
						}) // 用户端首页
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;
		// padding-top: 120rpx;

		.title {}


		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content::before {
			content: '';
			position: absolute;
			width: 100%;
			height: 200rpx;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			background: rgba(255, 255, 255, 0.23);
			border-radius: 20px 20px 0px 0px;
			backdrop-filter: blur(10px);
			// z-index: -1;
		}

		.login-form-content {
			position: relative;
			top: -160rpx;
			border-radius: 20px 20px 0px 0px;
			/* 纯白 */
			border: 1px solid #FFFFFF;
			padding: 140rpx 60rpx 60rpx;
			width: 100%;

			.input-item {
				.label {
					font-size: 32rpx;
					width: 120rpx;
					color: #323232;
				}

				border-bottom: 1px solid rgba(0, 0, 0, 0.05);
				;
				margin: 20px auto;
				// background-color: #f5f6f7;
				height: 45px;
				// border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					flex: 1;
					// width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					// padding-left: 15px;
				}

			}

			.login-btn {
				// background: linear-gradient(270deg, #2C84FB 0%, rgba(44, 132, 251, 0.42) 100%);
				margin-bottom: 30rpx;
				// margin-top: 40px;
				height: 70rpx;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}

	.top_img {
		padding: 140rpx 60rpx;
		width: 100%;
		height: 520rpx;
		background-image: url('https://beihai-food.oss-cn-beijing.aliyuncs.com/01.png');
		background-size: contain;
		background-repeat: no-repeat;
		font-size: 36rpx;
		font-weight: 500;
	}

	.u-page__code-text {
		color: $uni-color-primary;
	}

	.action-btn {
		margin-top: 80rpx;
	}
</style>